<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="public/layui/css/layui.css" media="all">
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body style="padding:9px">
<div class="layui-form layui-form-pane" id="ability-form">
    <div class="layui-form-item">
        <label class="layui-form-label">能力编码</label>
        <div class="layui-input-inline">
            <input type="text" name="name" lay-verify="required|name" autocomplete="off" placeholder="请输入能力编码"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">请求类型</label>
        <div class="layui-input-inline">
            <select name="method">
                <option value="GET">GET</option>
                <option value="POST" selected="">POST</option>
                <option value="PUT">PUT</option>
                <option value="DELETE">DELETE</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">服务类型</label>
            <div class="layui-input-inline" style="width:100px">
                <select name="serviceType">
                    <option value="0">微服务</option>
                    <option value="1">http url服务</option>
                </select>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">服务编码</label>
            <div class="layui-input-inline">
                <input type="text" name="serviceName" lay-verify="required|serviceName" value="1200"
                       placeholder="serviceName" autocomplete="off" class="layui-input">
            </div>

        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">超时时间</label>
        <div class="layui-input-inline">
            <input type="text" name="timeout" lay-verify="required|number" value="9000" autocomplete="off"
                   class="layui-input">
        </div>
        <div class="layui-form-mid layui-word-aux">超时时间(ms毫秒)</div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">限流设置</label>
        <div class="layui-input-inline">
            <input type="text" name="rateLimit" lay-verify="required|number" value="-1" placeholder="-1 不限流"
                   autocomplete="off" class="layui-input">
        </div>
        <div class="layui-form-mid layui-word-aux">限流(分钟) (<=0 不限制)</div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">限流设置</label>
        <div class="layui-input-inline">
            <input type="text" name="perRateLimit" lay-verify="required|number" value="-1"  placeholder="-1 不限流"
                   autocomplete="off" class="layui-input">
        </div>
        <div class="layui-form-mid layui-word-aux">每个用户限流(<=0 不限制)</div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">超时次数</label>
        <div class="layui-input-inline">
            <input type="text" name="maxTimeoutTimes" lay-verify="number" value="-1"  placeholder="-1 不限制"
                   autocomplete="off" class="layui-input">
        </div>
        <div class="layui-form-mid layui-word-aux">的超时次数（分钟）(<=0 不限制)</div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">超时次数</label>
        <div class="layui-input-inline">
            <input type="text" name="perMaxTimeoutTimes" lay-verify="number" value="-1"  placeholder="-1 不限制"
                   autocomplete="off" class="layui-input">
        </div>
        <div class="layui-form-mid layui-word-aux">每个用户的超时次数 (<=0 不限制)</div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">是否公开</label>
        <div class="layui-input-inline" style="width:80px">
            <input type="checkbox" name="isPrivate"  id="isPrivate" lay-filter="isPrivate" value="1" lay-verify="isPrivate" lay-skin="switch" lay-text="公共|私有">
        </div>
        <div class="layui-form-mid layui-word-aux">（公开）所有用户都可以调用;（私有）vip用户或授权普通用户</div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">是否上线</label>
        <div class="layui-input-block">
            <input type="checkbox" name="state"  id="state" lay-filter="state"  lay-verify="state" lay-skin="switch" lay-text="启用|关闭">
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">能力简要描述</label>
        <div class="layui-input-block">
            <textarea placeholder="请输入内容" name="description" class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item" style="text-align: center">
        <button class="layui-btn" id="saveBtn" lay-submit="" lay-filter="saveBtn">立即提交</button>
    </div>
</div>


<script src="public/layui/layui.js" charset="utf-8"></script>
<script src="js/jquery-3.2.1.js"></script>
<script src="js/loader.js"></script>
<script>
    layui.use(['form', 'layedit', 'laydate', 'oka'], function () {
        var form = layui.form
            , layer = layui.layer
            , layedit = layui.layedit
            , laydate = layui.laydate
            , oka = layui.oka;
        //alert(location.hash);
        //自定义验证规则

        var hash = location.hash;
        if (hash != "") {
            var readonly = false;
            hash = hash.substring(1);
            if(hash.substring(0,1) == "!") {
                hash = hash.substring(1);
                readonly = true;
            }
            oka.loading.open();
            $.ajax({
                url: "/admin/ability/" + hash
                , type: "GET"
                , async: true
                , success: function (res) {
                    oka.loading.close();
                    if(!res)
                        return;
                    //obj = null;

                    var data = res;

                    if(data.state == "on") {
                        $("#state").prop("checked", true);
                    }
                    if(data.isPrivate == true) {
                        $("#isPrivate").prop("checked", true);
                    }
                    $('#ability-form').find('[name]').each(function () {
                        var s = data[$(this).attr('name')];

                        if (s != undefined) {
                           // console.log($(this).attr('name'))
                            if(readonly) {
                                $(this).attr("disabled",true);
                            }

                            $(this).val(s);
                        }

                    });
                    if(readonly) {
                        $("#saveBtn").attr("disabled",true);
                        $("#saveBtn").addClass("layui-btn-disabled");
                    }
                    form.render();
                }
            });
        }

        form.verify({
            title: function (value) {
                if (value.length < 5) {
                    return '标题至少得5个字符啊';
                }
            }
            , content: function (value) {
                alert(value)
            }
        });

        form.on('switch(state)', function (data) {
            $("#state").prop("value", this.checked ? 'on' : 'off');
        });
        form.on('switch(isPrivate)', function (data) {
            $("#isPrivate").prop("value", this.checked ? true : false);
        });
        //监听提交

        form.on('submit(saveBtn)', function (data) {
            oka.loading.open();
           // console.log(data.field.isPrivate)
            $.ajax({
                url: "/admin/ability" + (hash != "" ? ("/" + hash) : "")
                , type: hash != "" ? "PUT" : "POST"
                , dataType: "json"
                , contentType: "application/json"
                , async: false
                , data: JSON.stringify(data.field)
                , success: function (res) {
                    oka.loading.close();
                    if (res.code == "0") {

                        layer.msg('操作失败!!！', {
                            offset: "t",
                            anim: 6
                        });

                    } else {

                        layer.msg('操作成功',{
                            time: 600
                        },function(){
                            var ret = {
                                description: data.field.description,
                                state: data.field.state?data.field.state:"off",
                                name: data.field.name
                            }
                            if(res.bizDesc != "") {
                                ret.id = res.bizDesc;
                            }
                            parent.oka.page.ret = JSON.stringify(ret);
                            parent.layer.close(parent.layer.index);
                        });

                    }
                }
            });


            return false;
        });


    });
</script>

</body>
</html>